﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;

using Xyz.Web.Reservation.AddonService;
using Xyz.Web.Reservation.AircraftSeatService;
using Xyz.Web.Reservation.BookingService;
using Xyz.Web.Reservation.CityService;
using Xyz.Web.Reservation.CountryService;
using Xyz.Web.Reservation.CustomerService;
using Xyz.Web.Reservation.FlightPriceService;
using Xyz.Web.Reservation.PassengerService;

namespace Xyz.Web.Reservation.Repositories
{
    public class BaseRepository
    {
        protected static AddonServiceClient AddonServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["AddonServiceClient"] == null)
                    HttpContext.Current.Session["AddonServiceClient"] = new AddonServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["AddonServiceClient"] as AddonServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new AddonServiceClient();
                    HttpContext.Current.Session["AddonServiceClient"] = client;
                }

                return client;
            }
        }

        protected static AircraftSeatServiceClient AircraftSeatServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["AircraftSeatServiceClient"] == null)
                    HttpContext.Current.Session["AircraftSeatServiceClient"] = new AircraftSeatServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["AircraftSeatServiceClient"] as AircraftSeatServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new AircraftSeatServiceClient();
                    HttpContext.Current.Session["AircraftSeatServiceClient"] = client;
                }

                return client;
            }
        }

        protected static BookingServiceClient BookingServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["BookingServiceClient"] == null)
                    HttpContext.Current.Session["BookingServiceClient"] = new BookingServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["BookingServiceClient"] as BookingServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new BookingServiceClient();
                    HttpContext.Current.Session["BookingServiceClient"] = client;
                }

                return client;
            }
        }

        protected static CityServiceClient CityServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["CityServiceClient"] == null)
                    HttpContext.Current.Session["CityServiceClient"] = new CityServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["CityServiceClient"] as CityServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new CityServiceClient();
                    HttpContext.Current.Session["CityServiceClient"] = client;
                }

                return client;
            }
        }

        protected static CountryServiceClient CountryServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["CountryServiceClient"] == null)
                    HttpContext.Current.Session["CountryServiceClient"] = new CountryServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["CountryServiceClient"] as CountryServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new CountryServiceClient();
                    HttpContext.Current.Session["CountryServiceClient"] = client;
                }

                return client;
            }
        }

        protected static CustomerServiceClient CustomerServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["CustomerServiceClient"] == null)
                    HttpContext.Current.Session["CustomerServiceClient"] = new CustomerServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["CustomerServiceClient"] as CustomerServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new CustomerServiceClient();
                    HttpContext.Current.Session["CustomerServiceClient"] = client;
                }

                return client;
            }
        }

        protected static FlightPriceServiceClient FlightPriceServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["FlightPriceServiceClient"] == null)
                    HttpContext.Current.Session["FlightPriceServiceClient"] = new FlightPriceServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["FlightPriceServiceClient"] as FlightPriceServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new FlightPriceServiceClient();
                    HttpContext.Current.Session["FlightPriceServiceClient"] = client;
                }

                return client;
            }
        }

        protected static PassengerServiceClient PassengerServiceClient
        {
            get
            {
                // Check if not initialized yet
                if (HttpContext.Current.Session["PassengerServiceClient"] == null)
                    HttpContext.Current.Session["PassengerServiceClient"] = new PassengerServiceClient();

                // If current client is 'faulted' (due to some error), create a new instance.
                var client = HttpContext.Current.Session["PassengerServiceClient"] as PassengerServiceClient;
                if (client.State == CommunicationState.Faulted)
                {
                    try { client.Abort(); }
                    catch { /* no action */ }

                    client = new PassengerServiceClient();
                    HttpContext.Current.Session["PassengerServiceClient"] = client;
                }

                return client;
            }
        }
    }
}